package com.shi.service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;

import javax.mail.MessagingException;

import com.shi.dao.ExercisesDao;
import com.shi.pojo.Exercises;
import com.shi.pojo.Role;
import com.shi.pojo.User;

public class UserService {
	ExercisesDao dao = new ExercisesDao();


	public void deleteExercise(String id) {
		// TODO Auto-generated method stub
		
	}

	public void changeExercise(String id, String answer) {
		
	}
	public void regist(String username, String password, String repassword,String email, String roleId, String userId) throws Exception, MessagingException {
		User form = new User();
		Role role = new Role();
		form.setPassword(password);
		form.setUserName(username);
		form.setEmail(email);
		Date date=new Date();
	    DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	    String time=format.format(date);
		form.setRegisterTime(time);
		form.setRoleId(roleId);
		form.setUserId(userId);
		if(dao.queryByUsername(form.getUserName()).getUserName() != null){
			throw new Exception("用户名已存在");
		}else if(dao.queryByEmail(form.getEmail()).getUserName() != null){
			throw new Exception("邮箱已注册");
		}
		role.setRoleId(roleId);
		role.setRoleName("普通用户");
		dao.addUser(form);
		dao.addRole(role);
	}
	public User login(String username, String password) throws Exception {
		User user = dao.queryByUsername(username);
		if(user.getUserName() == null){
			throw new Exception("用户不存在");
		}else if(user.getUserName() != null){
			if(user.getPassword().equals(password)){
					return user;
			}else{
				throw new Exception("密码错误");
			}
		}else{
			throw new RuntimeException("未知错误");
		}
	}

	public void addExercise(String id, String title, String type, String a,
			String b, String c, String d, String answer, String uploadUserId) {
		dao.addExercise(id, title,type,a, b,c,d,answer,uploadUserId);
	}

	public int checkAnswer(List<Exercises> forms,String userId) throws Exception {
		int score = 0;
		List<Exercises> exercises = dao.queryExercisesById(forms);
		for(Exercises form : forms){
			for(Exercises exercise : exercises){
				if(exercise.getExercisesId().equals(form.getExercisesId())){
					String qString = exercise.getAnswer();
					String w = form.getAnswer();
					if(exercise.getAnswer().equals(form.getAnswer())){
						score += 10;
					}else{
						dao.addWrongExercise(form.getExercisesId(),userId,form.getAnswer(),exercise.getAnswer(),form.getTitle());
					}
					exercises.remove(exercise);
					break;
				}
			}
		}
		return score;
	}
	
	public List<Exercises> doiongExercises() {
		int size = dao.findExercisesSize();
		Random random=new Random();
		int start = random.nextInt(10);
		int end = 0;
		String[] tags = new String[10];
		tags[0] = "one";
		tags[1] = "two";
		tags[2] = "three";
		tags[3] = "four";
		tags[4] = "five";
		tags[5] = "six";
		tags[6] = "seven";
		tags[7] = "eight";
		tags[8] = "nine";
		tags[9] = "ten";
		int i = 0;
		if (start + 10 >= size) {
			start = size - 10;
		}
		end = start + 10;
		List<Exercises> exercises = dao.findExercises(start,end);
		for(Exercises e : exercises){
			e.setTag(tags[i++]);
		}
		return exercises;

	}

	public List<Exercises> findWrongExerciseByUser(String userId) {
		List<Exercises> exercises = dao.findWrongExerciseByUser(userId);
		return exercises;
	}

	public Exercises findWrongExerciseByExerciseId(String exercisesId,String userId) {
		Exercises exercise = dao.findWrongExerciseByExerciseId(exercisesId,userId);
		return exercise;
	}

	public int addExercise(String exercisesId, String userId) {
		return dao.addExerciseByExerciseIdAndUserId(exercisesId,userId);
	}

	public List<Exercises> findMyExercise(String userId) {
		// TODO Auto-generated method stub
		List<Exercises> exercises = dao.findMyExercisesByUserId(userId);
		return exercises;
	}

	public int removeExercise(String exercisesId, String userId) {
		return dao.removeExerciseByExerciseIdAndUserId(exercisesId,userId);
	}

}
